import { MaterialManageAPI } from '../common/type';
import { Button, Tooltip } from 'antd';
import { getPosIDs, getUserID, removeDateHmsStr, tooltipLength } from '@/utils';
import pageStyles from '@/common/pageLayout.less';
import { Fragment } from 'react';
import { EditOutlined, EyeOutlined, CloseCircleOutlined, DeliveredProcedureOutlined } from '@ant-design/icons';
import { stateOfRendering } from '../common';
import { ManageCommonAPI } from '@/pages/type';
import { tableMoreBtnConfig } from '@/utils/myutilConfig';
import { ColumnsRenderFun, DictionaryRenderFun, formatDecimal } from '../../Acommon';

/**操作按钮的显示*/
const Operation = (curRes: Record<string, any>, operateFuns: MaterialManageAPI.IMaterialTableActionType, record: Record<string, any>) => {
  let operationArray: any = [];

  /** 一个按钮都没有时展示*/
  const Disable = (
    <Fragment key={record?.id + '--Disable'}>
      <Button style={{ color: '#000' }} className="manageBtntTxtStyle" type={'link'} size={'small'} disabled>
        --
      </Button>
    </Fragment>
  );

  /** 更多按钮配置 */
  const otherBtnConfig: ManageCommonAPI.TableMoreBtnConfigApi[] = [
    {
      icon: <EyeOutlined />,
      btnName: '查看',
      clickFnc: operateFuns,
      clickType: 'onView',
      isVisible: false,
    },
    {
      icon: <EditOutlined />,
      btnName: '编辑',
      clickFnc: operateFuns,
      clickType: 'onEdit',
      isVisible: curRes?.['/materialController/edit'] && record?.state == 0 && record?.createPositionID == getPosIDs() && record?.createrID == getUserID(),
    },
    {
      icon: <DeliveredProcedureOutlined />,
      btnName: '申请',
      clickFnc: operateFuns,
      clickType: 'onApply',
      isVisible: curRes?.['/materialController/applyMaterial'] && record?.state == 0,
    },
    {
      icon: <CloseCircleOutlined />,
      btnName: '作废',
      clickFnc: operateFuns,
      clickType: 'onCancel',
      isVisible:
        curRes?.['/materialController/cancel'] &&
        (record?.state == 0 || record?.state == 2) &&
        record?.createPositionID == getPosIDs() &&
        record?.createrID == getUserID(),
    },
  ];
  const ohterBtn = tableMoreBtnConfig(otherBtnConfig, record);
  operationArray = ohterBtn;
  operationArray = operationArray?.filter((itemBtn: any) => itemBtn);
  return operationArray && operationArray?.length > 0 ? operationArray : [Disable];
};

// Table表格的配置
const getTableColumns = (curRes: Record<string, any>, operateFuns: MaterialManageAPI.IMaterialTableActionType) => {
  const { onView } = operateFuns;
  return [
    {
      title: '物料编码',
      dataIndex: 'materialNum',
      key: 'materialNum',
      width: 180,
      visible: true,
      unDrag: true,
      fixed: 'left',
      sorter: (a: any, b: any) => (a?.id > b?.id ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text, wordNum: 100, isTooltip: true }),
    },
    {
      title: '物料名称',
      dataIndex: 'fullName',
      key: 'fullName',
      fixed: 'left',
      width: 180,
      unDrag: true,
      visible: true,
      sorter: (a: any, b: any) => (a?.fullName > b?.fullName ? 1 : -1),
      render: (text: string | null, record: Record<string, any>) => {
        return (
          <a
            onClick={() => {
              if (curRes?.['/materialController/details/{params}']) {
                onView(record);
              }
            }}
            style={{ color: '100%' }}
          >
            <Tooltip title={tooltipLength(text, 50)}>
              <span className={pageStyles['over-flow-hidden']}>{text || '--'}</span>
            </Tooltip>
          </a>
        );
      },
    },
    {
      title: '物料类型',
      dataIndex: 'materialTypeName',
      key: 'materialTypeName',
      visible: true,
      width: 140,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.materialType > b?.materialType ? 1 : -1),
      render: (text: string | null) => DictionaryRenderFun({ text }),
    },
    {
      title: '规格要求（参数）',
      dataIndex: 'specInfo',
      key: 'specInfo',
      visible: true,
      width: 185,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.specInfo > b?.specInfo ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '规格型号',
      dataIndex: 'modelNum',
      key: 'modelNum',
      visible: true,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.modelNum > b?.modelNum ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '物料状态', //0：起草 1：审核中 2：有效 3：作废
      dataIndex: 'state',
      key: 'state',
      visible: true,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.state > b?.state ? 1 : -1),
      render: (text: number | null) => stateOfRendering(text),
    },
    {
      title: '供货分类',
      dataIndex: 'supplyTypeName',
      key: 'supplyTypeName',
      visible: true,
      width: 130,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.supplyType > b?.supplyType ? 1 : -1),
      render: (text: string | null) => DictionaryRenderFun({ text }),
    },
    {
      title: '量化单位',
      dataIndex: 'unitBusinessName',
      key: 'unitBusinessName',
      visible: true,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.unitBusiness > b?.unitBusiness ? 1 : -1),
      render: (text: string | null) => DictionaryRenderFun({ text }),
    },
    {
      title: '参考单价（元）',
      dataIndex: 'unitPrice',
      key: 'unitPrice',
      visible: true,
      width: 180,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.unitPrice > b?.unitPrice ? 1 : -1),
      render: (text: string | null) => <span>{formatDecimal(text)}</span>,
    },
    {
      title: '物料简介',
      dataIndex: 'paramsInfo',
      key: 'paramsInfo',
      visible: true,
      width: 180,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.paramsInfo > b?.paramsInfo ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text, wordNum: 100, isTooltip: true }),
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      key: 'createTime',
      visible: true,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.createTime > b?.createTime ? 1 : -1),
      render: (text: string) => <span>{removeDateHmsStr(text)}</span>,
    },
    {
      title: '创建人公司',
      dataIndex: 'createCompanyName',
      key: 'createCompanyName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.createCompanyID > b?.createCompanyID ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '创建人部门',
      dataIndex: 'createDepartmentName',
      key: 'createDepartmentName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.createDepartmentName > b?.createDepartmentName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '创建人岗位',
      dataIndex: 'createPositionName',
      key: 'createPositionName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.createPositionName > b?.createPositionName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '创建人',
      dataIndex: 'createrName',
      key: 'createrName',
      visible: true,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.createrName > b?.createrName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '签发时间',
      dataIndex: 'signTime',
      key: 'signTime',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.signTime > b?.signTime ? 1 : -1),
      render: (text: string) => <span>{removeDateHmsStr(text)}</span>,
    },
    {
      title: '签发人公司',
      dataIndex: 'signCompanyName',
      key: 'signCompanyName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.signCompanyName > b?.signCompanyName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '签发人部门',
      dataIndex: 'signDepartmentName',
      key: 'signDepartmentName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.signDepartmentName > b?.signDepartmentName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '签发人岗位',
      dataIndex: 'signPositionName',
      key: 'signPositionName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.signPositionName > b?.signPositionName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '签发人',
      dataIndex: 'signerName',
      key: 'signerName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.signerName > b?.signerName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '修改时间',
      dataIndex: 'modifyTime',
      key: 'modifyTime',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.modifyTime > b?.modifyTime ? 1 : -1),
      render: (text: string) => <span>{removeDateHmsStr(text)}</span>,
    },
    {
      title: '修改人公司',
      dataIndex: 'modifyCompanyName',
      key: 'modifyCompanyName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.modifyCompanyName > b?.modifyCompanyName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '修改人部门',
      dataIndex: 'modifyDepartmentName',
      key: 'modifyDepartmentName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.modifyDepartmentName > b?.modifyDepartmentName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '修改人岗位',
      dataIndex: 'modifyPositionName',
      key: 'modifyPositionName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.modifyPositionName > b?.modifyPositionName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '修改人',
      dataIndex: 'modifyerName',
      key: 'modifyerName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.modifyerName > b?.modifyerName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '作废类型', //数据字典
      dataIndex: 'cancelTypeName',
      key: 'cancelTypeName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.cancelTypeName > b?.cancelTypeName ? 1 : -1),
      render: (text: string | null) => DictionaryRenderFun({ text }),
    },
    {
      title: '作废原因',
      dataIndex: 'cancelReason',
      key: 'cancelReason',
      visible: false,
      width: 180,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.cancelReason > b?.cancelReason ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '作废时间',
      dataIndex: 'cancelTime',
      key: 'cancelTime',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.cancelTime > b?.cancelTime ? 1 : -1),
      render: (text: string) => <span>{removeDateHmsStr(text)}</span>,
    },
    {
      title: '作废人公司',
      dataIndex: 'cancelCompanyName',
      key: 'cancelCompanyName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.cancelCompanyName > b?.cancelCompanyName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '作废人部门',
      dataIndex: 'cancelDepartmentName',
      key: 'cancelDepartmentName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.cancelDepartmentName > b?.cancelDepartmentName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '作废人岗位',
      dataIndex: 'cancelPositionName',
      key: 'cancelPositionName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.cancelPositionName > b?.cancelPositionName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '作废人',
      dataIndex: 'cancelerName',
      key: 'cancelerName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.cancelerName > b?.cancelerName ? 1 : -1),
      render: (text: string | null) => ColumnsRenderFun({ text }),
    },
    {
      title: '操作',
      dataIndex: 'operation',
      key: 'operation',
      width: 230,
      visible: true,
      unDrag: true,
      fixed: 'right',
      render: (text: string | null, record: Record<string, any>) => {
        return <div style={{ display: 'flex' }}>{Operation(curRes, operateFuns, record)}</div>;
      },
    },
  ];
};

export { getTableColumns };
